<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 10:52
 */

namespace app\api\business;


use app\api\controller\Common;
use think\Exception;
use think\facade\Db;

class CashBackBus extends Common
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 返回今天还未返现的列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCashList(){
        $list = Db::name('cashback')
            ->where('status', 0)
            ->where('update_time','<',date('Y-m-d'))
            ->whereOr('update_time','=',null)
            ->group('user_id')
            ->select()->toArray();
        return $list;
    }


    /**
     * 获取团队有效礼包数
     * @param $childrenIds
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTeamCount($childrenIds){
        $count = Db::name('cashback')
            ->where('status',0)
            ->whereIn('user_id', $childrenIds)
            ->value('sum(number)');
        return $count;
    }


    /**
     * 获取下级用户ids
     * @param $user_id
     * @return string
     * @throws Exception
     */
    public function getNextUserIds($user_id){
        try{
            // 直属下级总数
            $underlingCount = Db::name('user')->where('pid',$user_id)->count();
            // 当前用户层级
            $lvl =  Db::name('user')->where('user_id',$user_id)->value('lvl');
            $lvl = $lvl + 7;
            // 如果直属下级满六人则包括直属下级在内算起，往后6个级都是我的分销团队成员，否则只算直属下级
            if ($underlingCount > 5){ // 此处应大于5
                $userArr =  Db::name('user')
                    ->field('user_id,pid')
                    ->order('user_id','asc')
                    ->whereFindInSet('tree', $user_id)
                    ->where('lvl','<',$lvl)
                    ->select()->toArray();
            }else{
                $userArr =  Db::name('user')
                    ->field('user_id')
                    ->where('pid',$user_id)
                    ->select()->toArray();
            }
            $str = '';
            foreach ($userArr as $k => $v){
                $str .= $v['user_id'].',';
            }
            $str = rtrim($str,',');
            return $str;
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }

    }

    /**
     * 获取所有下级用户
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNextUsers($user_id){
        $userArr =  Db::name('user')
            ->field('user_id')
            ->order('user_id','asc')
            ->whereFindInSet('tree', $user_id)
            ->select()->toArray();
        return $userArr;
    }

}